package cate.game.admin;

import cate.game.activity.ActivityMenu;
import cate.game.admin.menu.bag.BagMenu;
import cate.game.admin.menu.chat.ChatMenu;
import cate.game.admin.menu.cross.CrossMenu;
import cate.game.admin.menu.custom.CustomMenu;
import cate.game.admin.menu.demo.DemoMenu;
import cate.game.admin.menu.farm.FarmMenu;
import cate.game.admin.menu.fight.FightMenu;
import cate.game.admin.menu.furniture.FurnitureMenu;
import cate.game.admin.menu.guild.GuildMenu;
import cate.game.admin.menu.hang.HangMenu;
import cate.game.admin.menu.hero.HeroMenu;
import cate.game.admin.menu.mail.MailMenu;
import cate.game.admin.menu.mall.MallMenu;
import cate.game.admin.menu.pay.PayMenu;
import cate.game.admin.menu.pvp.LadderMenu;
import cate.game.admin.menu.pvp.MinecraftMenu;
import cate.game.admin.menu.pvp.PvpMenu;
import cate.game.admin.menu.pvp.TBMenu;
import cate.game.admin.menu.rank.RankMenu;
import cate.game.admin.menu.res.ResMenu;
import cate.game.admin.menu.role.RoleMenu;
import cate.game.admin.menu.rpc.RpcMenu;
import cate.game.admin.menu.show.ShowMenu;
import cate.game.admin.menu.sim.SimMenu;
import cate.game.admin.menu.task.TaskMenu;
import cate.game.admin.menu.tools.ToolsMenu;
import cate.game.admin.menu.wallet.WalletMenu;
import cate.game.framework.GameException;
import cate.game.framework.ModuleMan;
import cate.game.role.Role;

import java.util.*;

public class AdminManager extends ModuleMan {
    /** 永远都是A获胜 */
    private boolean alwaysWinA = false;

    private boolean needOriginal = false;

    public void setNeedOriginal(boolean needOriginal) {
        this.needOriginal = needOriginal;
    }

    public boolean isAlwaysWinA() {
        return alwaysWinA;
    }

    public void setAlwaysWinA(boolean alwaysWinA) {
        this.alwaysWinA = alwaysWinA;
    }

    private final Map<String, AdminMenu> menuMap = new HashMap<>();
    private final Map<String, AdminMenu> originalmenuMap = new HashMap<>();
    private final Map<Integer, String> sortLst = new HashMap<>();
    private final Map<Integer, String> sortLstOriginal = new HashMap<>();
    private int tmpk = 0;
    private int tmpl = 0;

    public void register(AdminMenu menu) {
        if (needOriginal) {
            originalmenuMap.put(menu.cmd, menu);
            sortLstOriginal.put(tmpl, menu.cmd);
            tmpl++;
        } else {
            menuMap.put(menu.cmd, menu);
            sortLst.put(tmpk, menu.cmd);
            tmpk++;
        }
    }

    public void runCmd(Role role, String cmd, List<String> params) {
        AdminMenu menu = menuMap.get(cmd);
        if (menu != null) {
            try {
                menu.getRunnable().run(role, params);
            } catch (GameException e) {
                role.getGame().notice.message(role, e.toResult());
                if(!e.silent) log.error("", e);
            } catch (Exception e) {
                role.getGame().notice.message(role, e.getMessage());
                log.error("", e);
            }

        } else {
            AdminMenu orgMenu = originalmenuMap.get(cmd);
            if (orgMenu != null) {
                try {
                    orgMenu.getRunnable().run(role, params);
                } catch (GameException e) {
                    role.getGame().notice.message(role, e.toResult());
                    if(!e.silent) log.error("", e);
                } catch (Exception e) {
                    role.getGame().notice.message(role, e.getMessage());
                    log.error("", e);
                }
            }
        }
    }

    public Collection<AdminMenu> getMenus() {
        return menuMap.values();
    }

    public List<AdminMenu> getMenus2(boolean isOriginal) {
        List<AdminMenu> lst = new ArrayList<>();
        if (isOriginal) {
            for (int i = 0; i < tmpl; i++) {
                lst.add(originalmenuMap.get(sortLstOriginal.get(i)));
            }
        } else {
            for (int i = 0; i < tmpk; i++) {
                lst.add(menuMap.get(sortLst.get(i)));
            }
        }

        return lst;
    }

    @Override
    public void initDependent() throws Exception {
        new BagMenu(game);
        new HeroMenu(game);
        new ChatMenu(game);
        new RoleMenu(game);
        new MailMenu(game);
        new FightMenu(game);
        new WalletMenu(game);
        new FarmMenu(game);
        new SimMenu(game);
        new GuildMenu(game);
        new TaskMenu(game);
        new PayMenu(game);
        new PvpMenu(game);
        new ResMenu(game);
        new ToolsMenu(game);
        new DemoMenu(game);
        new RpcMenu(game);
        new CustomMenu(game);
        new ActivityMenu(game);
        new HangMenu(game);
        new ShowMenu(game);
        new LadderMenu(game);
        new MinecraftMenu(game);
        new FurnitureMenu(game);
        new MallMenu(game);
        new RankMenu(game);
        new CrossMenu(game);
        new TBMenu(game);
    }
}
